package com.google.project.client.Director;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.project.client.Service.ProjTaskService;
import com.google.project.client.Service.ProjTaskServiceAsync;
import com.google.project.client.Service.ProjectService;
import com.google.project.client.Service.ProjectServiceAsync;
import com.google.project.shared.ProjectDTO;
import com.google.project.shared.ProjectTaskDTO;

public class Gantt extends Composite {

	private static final Binder binder = GWT.create(Binder.class);
	@UiField Button button;
	@UiField Button btnBack;
	@UiField Label lblProject;
	@UiField HTML html;

	private final ProjectServiceAsync projectService = GWT
			.create(ProjectService.class);
	private final ProjTaskServiceAsync ProjectTaskService = GWT.create(ProjTaskService.class);
	interface Binder extends UiBinder<Widget, Gantt> {
	}

	private String projectId;
	int count = 0;
	int load =0;
	public Gantt() {
		initWidget(binder.createAndBindUi(this));
		
	}

	public Gantt(String projectId) {
		initWidget(binder.createAndBindUi(this));
		
		Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand () {
	        public void execute () {
	        	button.setFocus(true);
	        }
	    });
		this.projectId = projectId;
		projectService.getProjectObject(projectId,
				new AsyncCallback<ProjectDTO>() {

					@Override
					public void onSuccess(ProjectDTO result) {
						// TODO Auto-generated method stub

						lblProject
								.setText("Dự án : " + result.getProjectName());
					}

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub

					}
				});
	}

	private native void smile(String data)
	/*-{
		$wnd.smile(data);
	}-*/;

	private native void drawGantt(String data)
	/*-{
		$wnd.smile(data);
	}-*/;

	public String convertToDate(Date input) {
		return (input.getMonth() + 1) + "/" + input.getDate() + "/"
				+ (input.getYear() + 1900);

	}

	@UiHandler("button")
	void onButtonClick(ClickEvent event) {

		count = 0;
	
		ProjectTaskService.getListOrderTaskByProjectId(projectId, new AsyncCallback<List<ProjectTaskDTO>>() {
			
			@Override
			public void onSuccess(List<ProjectTaskDTO> result1) {
				LinkedList<ProjectTaskDTO> listTask = new LinkedList<ProjectTaskDTO>();
				for(ProjectTaskDTO task : result1)
				{
//					if(task.getStatus()!=3)
//					{
					listTask.add(task);
					//}
				}
				
				
				
				String a="";
				// TODO Auto-generated method stub
				if(listTask!=null&&listTask.size()>0)
				{
					
				for(int i = 0;i<listTask.size();i++)
				{
					if(listTask.get(i).getParentId()==0)//&&result.get(i).getStatus()!=3
					{
						
						int taskId = listTask.get(i).getTaskId();
						String taskName = listTask.get(i).getTaskName();
						String startDate = convertToDate(new Date(listTask.get(i).getStartDate()));
						String endDate = convertToDate(new Date(listTask.get(i).getDueDate()));
						String color="";
					if(listTask.get(i).getStatus()!=3)
						{
						 color ="00ffff";
						}
					else
					{
						 color ="ff0000";
					}
						
						String link ="";
						int isMile = listTask.get(i).getIsMile();
//						String res = "ducnm";
//						int comp = 60;
						
						String res = listTask.get(i).getOptAccountName();
						
				        Double total =listTask.get(i).getAmountOfWork();
				        Double finish = listTask.get(i).getFinishedWork();
				        Double complete=0d;
				        if(listTask.get(i).getIsMile()==0)
				        {
				        	 complete = (finish/total)*100;
						
				        }
					
						int group = listTask.get(i).getGroup();
						int parent = listTask.get(i).getParentId();
						int open =1;
						String depen ="";
						
						
					   if(listTask.get(i).getDependent()!=null&&!listTask.get(i).getDependent().equalsIgnoreCase(""))
					   {
						   depen= listTask.get(i).getDependent().substring(0, listTask.get(i).getDependent().indexOf("|"));
					      // Window.alert(depen);
					   }
						String caption ="";
						if(count==(listTask.size()-1))
						{
							a= a+taskId+"_,"+taskName+"_,"+startDate+"_,"+endDate+"_,"+color+"_,"+link+"_,"+isMile+"_,"+res+"_,"+complete+"_,"+group+"_,"+parent+"_,"
							+open+"_,"+depen+"_,"+caption;
							count++;

						}

						else {
										a = a + taskId + "_," + taskName + "_,"
												+ startDate + "_," + endDate
												+ "_," + color + "_," + link
												+ "_," + isMile + "_," + res
												+ "_," + complete + "_,"
												+ group + "_," + parent + "_,"
												+ open + "_," + depen + "_,"
												+ caption + "|";
										count++;
									}
									a = drawChart(a, listTask, listTask.get(i)
											.getTaskId());
								}

							}
							smile(a);

						}

					}

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub

					}
				});

	}


	   public String drawChart(String data,List<ProjectTaskDTO> result,int parentId)
	   {
		   String temp="";
		   temp = data;
			for(int i = 0;i<result.size();i++)
			{
				if(result.get(i).getParentId()==parentId )
				{
					int taskId = result.get(i).getTaskId();
					String taskName = result.get(i).getTaskName();
					String startDate = convertToDate(new Date(result.get(i).getStartDate()));
					String endDate = convertToDate(new Date(result.get(i).getDueDate()));
					String color ="";
					if(result.get(i).getStatus()!=3)
					{
					 color ="00ffff";
					}
				else
				{
					 color ="ff0000";
				}
					String link ="";
					int isMile = result.get(i).getIsMile();
					String res = result.get(i).getOptAccountName();
					
			        Double total =result.get(i).getAmountOfWork();
			        Double finish = result.get(i).getFinishedWork();
			        Double complete=0d;
			        if(result.get(i).getIsMile()==0)
			        {
			        	 complete = (finish/total)*100;
					
			        }
					int group = result.get(i).getGroup();
					int parent = result.get(i).getParentId();
					int open =1;
					String depen ="";
					
					
					   if(result.get(i).getDependent()!=null&&!result.get(i).getDependent().equalsIgnoreCase(""))
					   {
						   depen= result.get(i).getDependent().substring(0, result.get(i).getDependent().indexOf("|"));
					      // Window.alert(depen);
					   }
					String caption ="";
					if(count==(result.size()-1))
					{ 
						temp= temp+taskId+"_,"+taskName+"_,"+startDate+"_,"+endDate+"_,"+color+"_,"+link+"_,"+isMile+"_,"+res+"_,"+complete+"_,"+group+"_,"+parent+"_,"
						+open+"_,"+depen+"_,"+caption;
						count++;
					}
					else
					{
						
						temp= temp+taskId+"_,"+taskName+"_,"+startDate+"_,"+endDate+"_,"+color+"_,"+link+"_,"+isMile+"_,"+res+"_,"+complete+"_,"+group+"_,"+parent+"_,"
						+open+"_,"+depen+"_,"+caption+"|";
						count++;
					}
					if(result.get(i).getGroup()==1)
					{
						temp=drawChart(temp,result,result.get(i).getTaskId());
					}
				}
				
			}
		   
		   return temp;
		   
	   }
	@UiHandler("btnBack")
	void onBtnBackClick(ClickEvent event) {
		DRTEntrypoint.get().DRTProjectDetail(projectId);
	}
}
